<?php

namespace App\Admin\Forms;

use Encore\Admin\Widgets\Form;
use Illuminate\Http\Request;
use App\Service\Model\Agency;

class AgencyForm extends Form
{
    
    /**
     * The form title.
     *
     * @var string
     */
    public $title = '新增代理';

    /**
     * Handle the form request.
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request)
    { 
        Agency::create([
            'tel'          => $request->tel,
            'name'         => $request->name,
            'password'     => bcrypt($request->password),
            'invite_code'  => $request->invite_code,
            'level'        => $request->parent_id == 0 ? 1: 2,
            'parent_id'    => $request->parent_id,
        ]);
        admin_success('操作成功');
        return back();
    }

    /**
     * Build a form here.
     */
    public function form()
    {
        $form = $this;
        $form->text('name', '姓名')->rules('required')->creationRules(['required', "unique:agency"]);
        $form->mobile('tel', '电话号码')->rules('required')->creationRules(['required', "unique:agency"]);
        $form->password('password', '密码')->rules('required|min:6|max:15',[
            'min'   => '最小不能少于6位',
            'max'   => '最大15位密码',
        ]);
        $form->text('invite_code','邀请码')->rules('required|max:6',[
            'max'   => '邀请码最大6位字符',
        ])->creationRules(['required', "unique:agency"]);
        $form->select('parent_id','选择上级代理')->options(Agency::getFirstLevelAgency());
        return $form;
     
    }

    /**
     * The data of the form.
     *
     * @return array $data
     */
    public function data()
    {
        return [
            'name'       => '',
           
        ];
    }
}
